kd-tree optimisation for knn in machine learning